#include <jni.h>
#include "mylog.h"
#include "SkCanvas.h"
#include "SkPaint.h"
#include "GraphicsJNI.h"
#include "SkBitmap.h"
#include "SkImageDecoder.h"
#ifdef __cplusplus
extern "C" {
#endif

SkBitmap* bm ;
void Java_com_jhondge_skia_view_SkiaView_init(JNIEnv *env,jobject thiszz,jstring path){
	LOGI("Java_com_jhondge_skia_view_SkiaView_init");
	bm = new SkBitmap;

	const char* filePath = env->GetStringUTFChars(path,false);
	//
	LOGI("Decoder image path is:%s",filePath);

	bool success = SkImageDecoder::DecodeFile(filePath,bm);
	
	LOGI("SkImageDecoder sucess:%d",success);

	env->ReleaseStringUTFChars(path,filePath);
}

jobject Java_com_jhondge_skia_view_SkiaView_loadBitmap(JNIEnv *env,jobject thiszz,jstring path){
	LOGI("============loadBitmap=============");
	const char* filePath = env->GetStringUTFChars(path,false);
	LOGI("loadBitmap path:%s",filePath);
	SkBitmap* bitmap = new SkBitmap;
	bool success = SkImageDecoder::DecodeFile(filePath,bitmap);
	LOGI("loadBitmap result:%d",success);
	jobject obj = GraphicsJNI::createBitmap(env,bitmap,false,NULL);
	LOGI("loadBitmap bitmap:%p",obj);
	env->ReleaseStringUTFChars(path,filePath);
	//delete bitmap;
	return obj;
}

void Java_com_jhondge_skia_view_SkiaView_renderSkia(JNIEnv *env, jobject thizz, jobject canvas){
	SkCanvas* canv = GraphicsJNI::getNativeCanvas(env, canvas);
    if (!canv){  
        return;  
    }  
    SkPaint paint;  
    paint.setColor(SK_ColorRED);  
	paint.setTextSize(40.5);

	int width = bm->width();
	int height = bm->height();

	LOGI("bmW:%d ,bmH:%d",width,height);
	//bm.
	LOGI("bmConfig:%p",bm->getConfig());

	canv->drawBitmap(*bm,0,0);
	canv->drawText("hello skia", 10, 100, 200, paint);  

}

void Java_com_jhondge_skia_view_SkiaView_release(JNIEnv *env,jobject thiszz){
	LOGI("Java_com_jhondge_skia_view_SkiaView_release");
	delete bm;
}
#ifdef __cplusplus
}
#endif